package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import com.workManager.base.BaseController;
import com.workManager.config.RespCode;
import com.workManager.pojo.TdOrganization;
import com.workManager.service.OrganizationService;
import com.workManager.vo.OrganizationVo;
import com.workManager.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2017/1/12 0012.
 */
@Controller
@RequestMapping("/organization")
public class OrganizationController extends BaseController{
    @Autowired
    private OrganizationService organizationService;


    @ApiOperation(value ="部门组织查询",notes = "部门组织查询")
    @RequestMapping(value = "/getOrganization",method = RequestMethod.POST)
    public Object findOrganization(){
        ModelMap modelMap = new ModelMap();
        return setSuccessModelMap(modelMap,organizationService.findOrganizationList());
    }

    @ApiOperation(value ="用户部门查询",notes = "查询用户的部门")
    @RequestMapping(value = "/queryUserOrganization",method = RequestMethod.POST)
    public Object queryUserOrganization(@RequestParam(value = "userId",required = true) String userId){
        ModelMap modelMap = new ModelMap();
        List<TdOrganization> organizationList = organizationService.queryUserOrganization(userId);
        return setSuccessModelMap(modelMap,organizationList);
    }

    @ApiOperation(value = "添加部门" ,notes = "添加部门")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody TdOrganization organization)
            throws Exception{

        ModelMap modelMap = new ModelMap();
/*
        String orgName = organization.getOrgName();
        Map map = new HashMap();
        map.put("orgName",orgName);

        PageInfo<OrganizationVo> organizationPageInfo = organizationService.query(map);
        if(organizationPageInfo.getTotal()>0){
            return  setModelMap(modelMap, RespCode.INTERNAL_SERVER_ERROR,"部门名："+orgName+" 已存在");
        }

        System.out.println("取得的值："+organizationPageInfo.getTotal());*/

        try{
            organizationService.add(organization);
        }catch (DuplicateKeyException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"部门名："+organization.getOrgName()+" 已存在！");
        }


        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value = "编辑部门" ,notes = "编辑部门")
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Object edit(@RequestBody TdOrganization organization)
            throws Exception{
        ModelMap modelMap = new ModelMap();


       /* Map map = new HashMap();
        map.put("orgName",organization.getOrgName());
        PageInfo<OrganizationVo> organizationPageInfo = organizationService.query(map);
        if(organizationPageInfo.getTotal()>1){
            return  setModelMap(modelMap, RespCode.INTERNAL_SERVER_ERROR,"部门名："+organization.getOrgName()+" 已存在");
        }*/
        Map<String,Object> reltMap  = null;
        try{
             reltMap=organizationService.edit(organization.getOrganizationId(),organization.getParentOrgId(),organization.getOrgName(),organization.getDescription());
        }catch (DuplicateKeyException e){
            return  setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"部门名:"+organization.getOrgName()+"已存在！");
        }



        return  setSuccessModelMap(modelMap,reltMap);
    }

    @ApiOperation(value = "删除部门" ,notes = "删除部门")
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public Object del(@ApiParam(required = true, value = "部门id") @RequestParam(value = "organizationIds", required = true) Integer[] organizationIds)
            throws Exception{
        ModelMap modelMap = new ModelMap();
        Assert.notNull(organizationIds, "部门id");
        Map<String,Object> reltMap = null;
        try {
            reltMap=organizationService.del(organizationIds);
        }catch (DataIntegrityViolationException e){
            return  setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"请先删除该部门及其子部门的用户");
        }
        return  setSuccessModelMap(modelMap,reltMap);
    }

    @ApiOperation(value = "部门查询接口" ,notes = "部门查询接口")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Object query(HttpServletRequest request,
                        @ApiParam(required = false, value = "页码") @RequestParam(value = "pageNumber",required = false) String pageNumber,
                        @ApiParam(required = false, value = "每页记录数") @RequestParam(value="pageSize", required = false) String pageSize,
                        @ApiParam(required = false, value = "部门名") @RequestParam(value="orgName", required = false) String orgName)throws Exception{

        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap<String, Object>();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);


        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("orgName",orgName);
        PageInfo<OrganizationVo> organizationPageInfo = organizationService.query(map);
        map.put("total",organizationPageInfo.getTotal());
        map.put("rows",organizationPageInfo.getList());
        return setSuccessModelMap(modelMap,map);
    }

}
